package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.AdvimgDto;
import com.itheima.ydd.entity.Advimg;
import com.itheima.ydd.entity.Advpos;
import com.itheima.ydd.service.AdvimgService;
import com.itheima.ydd.service.AdvposService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("ydd_advimg")
@Slf4j
public class AdvimgController {
	@Autowired
	private AdvimgService advimgService;

	@Autowired
	private AdvposService advposService;

	/**
	 * 广告图列表-分页
	 * @param advimgdesc
	 * @param advimgpos
	 * @param page
	 * @param limit
	 * @return
	 */
	@GetMapping("list")
	public ResultData list(String advimgdesc, Integer advimgpos, Integer page, Integer limit) {
		log.info("advimgdesc={} advimgpos={} page={}limit={}", advimgdesc, advimgpos, page, limit);
		Page<Advimg> pageInfo = new Page(page, limit);
		Page<AdvimgDto> pageDto = new Page<>();
		LambdaQueryWrapper<Advimg> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.like(StringUtils.isNotEmpty(advimgdesc), Advimg::getAdvimgdesc,advimgdesc);
		queryWrapper.like(advimgpos!=null, Advimg::getAdvimgpos,advimgpos);
		advimgService.page(pageInfo, queryWrapper);
		BeanUtils.copyProperties(pageInfo,pageDto,"records");
		List<Advimg> pageInfoList = pageInfo.getRecords();
		List<AdvimgDto> advimgDtoList = pageInfoList.stream().map(item -> {
			AdvimgDto advimgDto = new AdvimgDto();
			BeanUtils.copyProperties(item, advimgDto);
			int id = Integer.parseInt(item.getAdvimgpos());
			LambdaQueryWrapper<Advpos> advimgLambdaQueryWrapper = new LambdaQueryWrapper<>();
			advimgLambdaQueryWrapper.eq(Advpos::getId, id);
			Advpos advpos = advposService.getOne(advimgLambdaQueryWrapper);
			String advposname = advpos.getAdvposname();
			advimgDto.setAdvposname(advposname);
			return advimgDto;
		}).collect(Collectors.toList());
		Long count = Long.valueOf(advimgService.count(queryWrapper));
		Long totalPage = count / limit;
		Long pageSize = Long.valueOf(limit);
		return ResultData.ok("", PageResult.init(count,pageSize,totalPage,advimgDtoList));
	}

	/**
	 * 广告图详情
	 * @param id
	 * @return
	 */
	@GetMapping("rowInfo")
	public ResultData rowInfo(Integer id){
		Advimg advimg = advimgService.getById(id);
		return ResultData.ok("",advimg);
	}

	/**
	 * 广告图删除
	 * @param id
	 * @return
	 */
	@GetMapping("del")
	private ResultData delete(Integer id){
		advimgService.removeById(id);
		return ResultData.ok("","删除成功");
	}

	/**
	 * 广告图新增
	 * @param advimg
	 * @return
	 */
	@PostMapping("add")
	public ResultData add(@RequestBody Advimg advimg){
		advimgService.save(advimg);
		return ResultData.ok("","新增广告图成功");
	}

	/**
	 * 广告图修改
	 * @param advimg
	 * @return
	 */
	@PostMapping("update")
	public ResultData update(@RequestBody Advimg advimg){
		Integer id = advimg.getId();
		advimgService.updateById(advimg);
		return ResultData.ok("","修改成功");
	}

}
